package com.zxtyshangjia.zxtyjms.login.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zxtyshangjia.zxtyjms.MainActivity;
import com.zxtyshangjia.zxtyjms.R;
import com.zxtyshangjia.zxtyjms.base.BaseBean;
import com.zxtyshangjia.zxtyjms.login.bean.LoginBean;
import com.zxtyshangjia.zxtyjms.utils.Api;
import com.zxtyshangjia.zxtyjms.utils.SpUtils;
import com.zxtyshangjia.zxtyjms.utils.ToastUtil;
import com.zxtyshangjia.zxtyjms.utils.network.HttpUtil;
import com.zxtyshangjia.zxtyjms.utils.network.PostCallBack;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 18222 on 2017/11/13.
 * 登录页
 */

public class LoginActivity extends Activity {

    /**
     * 输入的账号
     */
    private EditText mAccountET;
    private String mAccount;

    /**
     * 输入的密码
     */
    private EditText mPasswordET;
    private String mPassword;


    /**
     * 登录 操作
     */
    private Button mLogin;


    private PostCallBack errorBack = new PostCallBack() {
        @Override
        public void onSuccess(Object data) {
            if(data != null && data instanceof BaseBean){
                BaseBean bean = (BaseBean) data;
                ToastUtil.showToast(bean.message);
            }
        }

        @Override
        public void onError(int code, String msg) {

        }
    };

    //登录回调
    private PostCallBack loginBack = new PostCallBack() {
        @Override
        public void onSuccess(Object data) {
            if(data != null && data instanceof LoginBean){
                LoginBean bean = (LoginBean) data;
                if(bean.flag.equals("success")){
                    ToastUtil.showToast(bean.message);

                    //登录成功跳转首页
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    SpUtils.getInstance(LoginActivity.this).save("is_Login",true);
                    finish();

                    //记录得到的加盟商的id
                    SpUtils.getInstance(LoginActivity.this).save("e_id",bean.data.e_id);
                    Log.e("e_id==========",bean.data.e_id);

                }
            }else {

                Map<String,String> map = new HashMap<String, String>();
                map.put("account",mAccount);
                map.put("password",mPassword);
                new HttpUtil(Api.LOGIN_URL,map, BaseBean.class,errorBack).postExecute();

            }
        }

        @Override
        public void onError(int code, String msg) {

        }
    };


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //判断商家是否已经登录 如果已经登录 则直接跳转首页 不显示登录页面

        if(SpUtils.getInstance(this).getBoolean("is_Login",false)){
            //已经登录过 直接跳转首页
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        setContentView(R.layout.activity_login);
        bindView();
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAccount = mAccountET.getText().toString();
                mPassword = mPasswordET.getText().toString();
                if(mAccount.equals("")){
                    ToastUtil.showToast("账号不能为空");
                }else if(mPassword.equals("")){
                    ToastUtil.showToast("请输入您的密码");
                }else {
                    //登录

                    Map<String,String> map = new HashMap<String, String>();
                    map.put("account",mAccount);
                    map.put("password",mPassword);
                    new HttpUtil(Api.LOGIN_URL,map, LoginBean.class,loginBack).postExecute();



                }
            }
        });
    }

    private void bindView() {
        mAccountET = (EditText) findViewById(R.id.accoutn_login_et);
        mPasswordET = (EditText) findViewById(R.id.password_login_et);
        mLogin = (Button) findViewById(R.id.login_bt);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //发送本地广播 关闭主页
            LocalBroadcastManager manager = LocalBroadcastManager.getInstance(this);
            manager.sendBroadcast(new Intent(MainActivity.class.getName()));
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
